/*
 * Created on Oct 18, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.erclab.internal.xpresso.reportelements;

import java.util.Properties;

import com.erclab.internal.xpresso.reports.Report;
import com.xpresso.utils.props.PropertyWorker;

/**
 * @author rodrigen
 *
 */
public class Link extends ReportElement {

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.reportelements.ReportElement#toHTML()
	 */
	public String toHTML(String columnValue, String uniqueKeyValue, Report theReport) {
		StringBuffer link = new StringBuffer();
		Properties props = this.getElementParameters();
		String url = props.getProperty("url");
		String displayValue = props.getProperty("displayValue");

		link.append("<a href=\"");
		if (columnValue != null){
			link.append(url.replaceAll("-param:columnValue-", columnValue));
		} else{
			link.append(url);
		}
			
		
		// Verify that the link contains a '?'
		if (url != null && url.indexOf('?') > 0){
			link.append("&uniqueKeyValue=");
		} else {
			link.append("?uniqueKeyValue=");
		}
		link.append(uniqueKeyValue);

		// TODO: PARENT KEY NAME is not used anymore, remove following 4 lines
		/*if (theReport.getParentKeyName() != null && !theReport.getParentKeyName().equals("")){
			link.append("&parentKeyValue=");
			link.append(theReport.getParameters().getParentKeyValue());
		}*/
		
		link.append("\">"); 
		link.append(displayValue.replaceAll("-param:columnValue-", columnValue));
		link.append("</a>");		
		return link.toString();
	}
	
	protected Properties getElementParameters(){
		return new PropertyWorker().getProperties(this.parameters);
	}

}
